<?php

class Img
{

    /** 图像里面增加文字
     * @param $source   图像路径
     * @param $file_path    保存文件路径
     * @param $text 文本信息
     * @param string $font
     * @return mixed
     */
    function mergeImg($source, $file_path, $text, $font = ''){
        if(empty($font)){
            $font = DATA_PATH.'font'.DIRECTORY_SEPARATOR.'cn.ttf';
        }

        $img = $file_path;
        $main = imagecreatefromjpeg($source);

        $width = imagesx($main);
        $height = imagesy($main);

        $target = imagecreatetruecolor($width, $height);

        $white = imagecolorallocate($target, 255, 255, 255);
        imagefill($target, 0, 0, $white);

        imagecopyresampled($target, $main, 0, 0, 0, 0, $width, $height, $width, $height);

        $fontSize = 22;//像素字体
        $fontColor = imagecolorallocate($target, 255, 0, 0);//字的RGB颜色

        if(!is_array($text)){
            $text = [$text];
        }

        $height = 80;
        $width = 100;
        foreach($text as $value){
//        $fontBox = imagettfbbox($fontSize, 0, $font, $value);//文字水平居中实质
            imagettftext($target, $fontSize, 0, $width, $height, $fontColor, $font, $value);
            $height += 40;
        }


        //imageantialias($target, true);//抗锯齿，有些PHP版本有问题，谨慎使用

//    imagefilledpolygon ( $target, array (10 + 0, 0 + 142, 0, 12 + 142, 20 + 0, 12 + 142), 3, $fontColor );//画三角形
//    imageline($target, 100, 200, 20, 142, $fontColor);//画线
//    imagefilledrectangle ( $target, 50, 100, 250, 150, $fontColor );//画矩形

        //bof of 合成图片
//    $child1 = imagecreatefromjpeg ( 'http://gtms01.alicdn.com/tps/i1/T1N0pxFEhaXXXxK1nM-357-88.jpg' );
//    imagecopymerge ( $target, $child1, 0, 400, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );
        //eof of 合成图片

        imagejpeg($target, $img, 95);

        imagedestroy($main);
        imagedestroy($target);
//    imagedestroy ( $child1 );
        return $img;
    }


    /** 创建图片
     * @param $file_path
     * @param $text
     * @param string $font
     * @return mixed
     */
    function generateImg($file_path, $text, $font = ''){
        if(empty($font)){
            $font = DATA_PATH.'font'.DIRECTORY_SEPARATOR.'cn.ttf';
        }

        $img = $file_path;
// 加载已经存在的图像
//        $main = imagecreatefromjpeg($source);

        //创建图片 并填充颜色
        $main = imagecreatetruecolor(800,800);
        $white = imagecolorallocate($main, 255, 255, 255);
        imagefill($main,0,0,$white);

        $width = imagesx($main);
        $height = imagesy($main);

        $target = imagecreatetruecolor($width, $height);

        $white = imagecolorallocate($target, 255, 255, 255);
        imagefill($target, 0, 0, $white);

        imagecopyresampled($target, $main, 0, 0, 0, 0, $width, $height, $width, $height);

        $fontSize = 22;//像素字体
        $fontColor = imagecolorallocate($target, 255, 0, 0);//字的RGB颜色

        if(!is_array($text)){
            $text = [$text];
        }

        $height = 80;
        $width = 100;
        foreach($text as $value){
//        $fontBox = imagettfbbox($fontSize, 0, $font, $value);//文字水平居中实质
            imagettftext($target, $fontSize, 0, $width, $height, $fontColor, $font, $value);
            $height += 40;
        }


        //imageantialias($target, true);//抗锯齿，有些PHP版本有问题，谨慎使用

//    imagefilledpolygon ( $target, array (10 + 0, 0 + 142, 0, 12 + 142, 20 + 0, 12 + 142), 3, $fontColor );//画三角形
//    imageline($target, 100, 200, 20, 142, $fontColor);//画线
//    imagefilledrectangle ( $target, 50, 100, 250, 150, $fontColor );//画矩形

        //bof of 合成图片
//    $child1 = imagecreatefromjpeg ( 'http://gtms01.alicdn.com/tps/i1/T1N0pxFEhaXXXxK1nM-357-88.jpg' );
//    imagecopymerge ( $target, $child1, 0, 400, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );
        //eof of 合成图片

        imagejpeg($target, $img, 95);

        imagedestroy($main);
        imagedestroy($target);
//    imagedestroy ( $child1 );
        return $img;
    }

    /** 根据数组生成对应的图片
     * @param array $data       数组
     * @param string $path      图片路径
     * @return bool|mixed       失败|图片url
     */
    public function create_array_text_img($data, $path){

        if(empty($data)){
            return false;
        }

        $text = [];

        $length = array_fill(0, count($data[0]), 0);


        foreach($data as $value){
            foreach($value as $k => $v){
                if($length[$k] < strlen($v)){
                    $length[$k] = strlen($v);
                }
            }
        }

        foreach($data as $value){
            $str = '';
            foreach($value as $k => $v){
                $str .= str_pad($v, $length[$k] + 2);
            }
            $text[] = $str;
        }


        return $this->generateImg ($path, $text);
    }


}
